package com.android.BluethootChess.rules;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Point;

import com.android.BluethootChess.model.GameBoardMatrix;
import com.android.BluethootChess.model.PieceColor;
import com.android.BluethootChess.util.Utils;

/**
 * RookRules clasa care implementeaza interfata PieceRulesInterface si defineste
 * regulile pentru piesa tura.
 * 
 * @see PieceRule
 */
public class NewRookRules extends PieceRule {

	public NewRookRules(GameBoardMatrix gameMatrix, PieceColor color) {
		super(gameMatrix, color);
	}

	// TODO Mai curata un pic si codu asta.
	public List<Point> findAllAvaiblePositions() {
		List<Point> list = new ArrayList<Point>();

		// Merge in dreapta pe linie.
		for (int i = piecePosition.y; i < this.matrix.length; i++) {
			if (matrix[piecePosition.x][i] == -1) {
				list.add(new Point(piecePosition.x, i));
			} else {
				int pieceCode = matrix[piecePosition.x][i];
				boolean cond=piecePosition.y!=i;
				if (Utils.getPieceColor(pieceCode) != pieceColor && cond) {
					list.add(new Point(piecePosition.x, i));
					break;
				}
				if (Utils.getPieceColor(pieceCode) == pieceColor && cond) {
					break;
				}
			}
		}
		
		
		// Merge in stanga pe linie.
		for (int i = piecePosition.y; i >= 0; i--) {
			if (matrix[piecePosition.x][i] == -1) {
				list.add(new Point(piecePosition.x, i));
			} else {
				int pieceCode = matrix[piecePosition.x][i];
				boolean cond=piecePosition.y!=i;
				if (Utils.getPieceColor(pieceCode) != pieceColor && cond) {
					list.add(new Point(piecePosition.x, i));
					break;
				}
				if (Utils.getPieceColor(pieceCode) == pieceColor && cond) {
					break;
				}
			}
		}
		
		
		// Merge in jos pe coloana.
		for (int i = piecePosition.x; i < this.matrix.length; i++) {
			if (matrix[i][piecePosition.y] == -1) {
				list.add(new Point(i, piecePosition.y));
			} else {
				int pieceCode = matrix[i][piecePosition.y];
				boolean cond=piecePosition.x!=i;
				if (Utils.getPieceColor(pieceCode) != pieceColor && cond) {
					list.add(new Point(i,piecePosition.y));
					break;
				}
				if (Utils.getPieceColor(pieceCode) == pieceColor && cond) {
					break;
				}
			}
		}
		
		// Merge in sus pe coloana.
		for (int i = piecePosition.x; i >= 0; i--) {
			if (matrix[i][piecePosition.y] == -1) {
				list.add(new Point(i, piecePosition.y));
			} else {
				int pieceCode = matrix[i][piecePosition.y];
				boolean cond=piecePosition.x!=i;
				if (Utils.getPieceColor(pieceCode) != pieceColor && cond) {
					list.add(new Point(i,piecePosition.y));
					break;
				}
				if (Utils.getPieceColor(pieceCode) == pieceColor && cond) {
					break;
				}
			}
		}
		
		return list;
	}
}
